package com.tarena.lbs.activity.service.impl;

import com.tarena.lbs.activity.dao.repository.ActivityRepository;
import com.tarena.lbs.activity.pojo.po.ActivityPO;
import com.tarena.lbs.activity.pojo.query.ActivityQuery;
import com.tarena.lbs.activity.pojo.vo.ActivityVO;
import com.tarena.lbs.activity.service.ActivityService;
import com.tarena.lbs.base.protocol.pager.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
@Service
public class ActivityServiceImpl implements ActivityService {

    private ActivityRepository activityRepository;
    @Autowired
    public void setActivityRepository(ActivityRepository activityRepository) {
        this.activityRepository = activityRepository;
    }

    public PageResult<ActivityVO> pageList(ActivityQuery query) {
        List<ActivityPO> po = activityRepository.selectAll(query);
        Long count = activityRepository.selectCount(query);
        List<ActivityVO> voList = po.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
        PageResult<ActivityVO> pageResult = new PageResult<>();
        pageResult.setObjects(voList);
        pageResult.setTotal(count);
        pageResult.setPageNo(query.getPageNo());
        pageResult.setPageSize(query.getPageSize());
        return pageResult;
    }

    private ActivityVO convertToVO(ActivityPO activityPO) {
        ActivityVO activityVO = new ActivityVO();
        BeanUtils.copyProperties(activityPO, activityVO);
        return activityVO;
    }
}
